﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Controller;
using Entity;

namespace Web
{
    public partial class EditCompanyInfo : System.Web.UI.Page
    {
        Helper helper = new Helper();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CompanyController companyController = new CompanyController();
                CompanyEntity companyEntity = companyController.FindCompany();
                CompanyNameTextBox.Text = companyEntity.CompanyName;
                PhoneTextBox.Text = companyEntity.Phone;
                FaxTextBox.Text = companyEntity.Fax;
                ContactPersonTextBoxt.Text = companyEntity.ContactPerson;
                ZipCodeTextBox.Text = companyEntity.ZipCode;
                AddressTextBox.Text = companyEntity.Address;
                //Session["FlashMessage"] = "Good Boy";
            }

            this.PreRender += new EventHandler(UpdateFlashMessage);
        }
        private void UpdateFlashMessage(object sender, EventArgs e)
        {
            helper.ShowFlashMessage(FlashMessageLabel);
        }

        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            try
            {
                CompanyEntity companyEntity = new CompanyEntity();
                companyEntity.CompanyName = CompanyNameTextBox.Text.Trim();
                companyEntity.Phone = PhoneTextBox.Text.Trim();
                companyEntity.Fax = FaxTextBox.Text.Trim();
                companyEntity.ContactPerson = ContactPersonTextBoxt.Text.Trim();
                companyEntity.Address = AddressTextBox.Text.Trim();
                companyEntity.ZipCode = ZipCodeTextBox.Text.Trim();
                CompanyController companyController = new CompanyController();
                int result = companyController.UpdateCompany(companyEntity);
                if (result == 0) //update successful
                {
                    //MsgLabel.Text = "Update successful!";
                    Session["FlashMessage"] = "Success: Update Successful!";
                    Session["FlashMessageType"] = "success";
                }
            }
            catch(Exception ex)
            {
                Session["FlashMessage"] = "Error: "+ex.Message;
                Session["FlashMessageType"] = "error";
            }
        }
    }
}